package service.Impl;

import dao.GoodsDao;
import dao.impl.GoodsDaoImpl;
import entity.Goods;
import entity.User;
import service.GoodsService;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    GoodsDao goodsDao=new GoodsDaoImpl();
    User user=new User();
    public boolean insertGoodsService(Goods goods){
        goodsDao.insertGoods(goods);
        System.out.println("商品上架成功！");
        return true;
    }
    public boolean deleteGoodsByGoodsIdService(String goodsId){
        boolean b=goodsDao.deleteGoodsByGoodsId( goodsId);
        if (b){
            System.out.println("下架成功！");
            return true;
        }else {
            System.out.println("查无此商品，下架失败！");
            return false;
        }
    }
    public boolean updateGoodsByIdService(String goodsId,Goods goods){
        List<Goods> goodsList=goodsDao.selectGoodsById(goodsId);
        if (goodsList!=null){
            for (int i=0;i<goodsList.size();i++){
                Goods goods1=goodsList.get(i);
                if (goods1.getGoodsId().equals(goodsId)){
                    goodsDao.updateGoodsById( goodsId, goods);
                    System.out.println("更改货物成功！");
                }
            }
            return true;
        }else {
            System.out.println("商品不存在，更改货物失败！");
            return false;
        }
    }
    public boolean buyGoodsByGoodsNameService(String goodsName,Goods goods){

        boolean b = goodsDao.buyGoodsByGoodsName(goodsName, goods);
        if (b){
            System.out.println("购买成功！");
            return true;
        }else {
            System.out.println("该商品不存在！");
            return false;
        }
    }
    public boolean returnGoodsByGoodsNameService(String goodsName,Goods goods){
        boolean a=goodsDao.returnGoodsByGoodsName(goodsName,goods);
        if (a){
            System.out.println("退货成功！");
            return true;
        }else {
            System.out.println("您未购买该商品");
            return false;
        }
    }
    public boolean changeGoodsByGoodsNameService(String goodsName,Goods goods){
        boolean c= goodsDao.changeGoodsByGoodsName(goodsName,goods);
        if (c){
            System.out.println("换货成功！");
            return true;
        }else {
            System.out.println("您未购买该商品");
            return false;
        }
    }

    @Override
    public Goods selectGoodsByGoodsNameService(String goodsName) {
        Goods goods = goodsDao.selectGoodsByGoodsName(goodsName);
        return goods;
    }

    public List<Goods> selectGoodsByIdService(String goodsId){
        List<Goods> goodsList=goodsDao.selectGoodsById(goodsId);
        if (goodsList!=null){
            return goodsList;
        }else {
            System.out.println("未查询到该商品");
            return null;
        }
    }
    public List<Goods> selectAllGoodsService() {
        List<Goods> goodsList = goodsDao.selectAllGoods();
        if (goodsList != null) {
            return goodsList;
        } else {
            System.out.println("无用户");
            return null;
        }
    }
}
